# -*- coding: utf-8 -*-

"""
 (c) 2023 - Copyright CTyunOS Inc

 Authors:
   youyifeng <youyf2@chinatelecom.cn>

"""

import logging
from optparse import OptionParser

from cve_ease import activate_session
from cve_ease.helper import one_instance_check
from cve_ease.models import CVELOG, SALOG

logger = logging.getLogger('cve-ease')


def get_usage_str(usage):
    return usage + "\n(Specify the --help global option for a list of other help options)"


def handle_logger(gconfig, db_session, args):
    """[basic] Log config tool"""

    # one instance
    lock = one_instance_check(gconfig.LOCK_FILE_PATH)
    if lock:
        print(f"Another ease already running")
        exit(1)

    usage = "usage: %prog logger <options>"
    parser = OptionParser(usage=get_usage_str(usage))

    parser.add_option('-l', '--list', dest='list', action='store_true', default=False,
                      help='list all logger info')
    parser.add_option('-t', '--total', dest='total', action='store_true', default=False,
                      help='get logger statistics')
    parser.add_option('-v', '--verbose', dest='verbose', action='store_true', default=False,
                      help='show verbose output')

    (options, args) = parser.parse_args(args)

    session = activate_session(db_session, gconfig)
    if gconfig.debug:
        print(" * active sql session")

    if options.total:
        cve_count = session.query(CVELOG).count()
        sa_count = session.query(SALOG).count()
        print("cve record num:", cve_count)
        print("sa record num:", sa_count)
        print("log expiration days:", gconfig.EXPIRATION_DAYS)
    elif options.list:
        cveloglist = session.query(CVELOG).all()
        if len(cveloglist) == 0:
            print(" WARNNING: no cve log found!")
        else:
            print("CVE LOG")
            print(
                '%-5s %-8s %-5s %-27s %-5s %-5s %-5s' % (
                    "ID", "RETCODE", "TOTAL", "TIMESTAMP", "ADD", "DELETE", "MODIFY"))
            for cvelog in cveloglist:
                print('%-5s %-8s %-5s %-27s %-5d %-5d %-5d' %
                      (
                          cvelog.id,
                          cvelog.code,
                          cvelog.totalCount,
                          cvelog.created_at,
                          cvelog.add,
                          cvelog.delete,
                          cvelog.modify
                      )
                      )

        print()
        saloglist = session.query(SALOG).all()
        if len(saloglist) == 0:
            print(" WARNNING: no sa log found!")
        else:
            print("SA LOG")
            print(
                '%-5s %-8s %-5s %-27s %-5s %-5s %-5s' % (
                    "ID", "RETCODE", "TOTAL", "TIMESTAMP", "ADD", "DELETE", "MODIFY"))
            for salog in saloglist:
                print('%-5s %-8s %-5s %-27s %-5d %-5d %-5d' %
                      (
                          salog.id,
                          salog.code,
                          salog.totalCount,
                          salog.created_at,
                          salog.add,
                          salog.delete,
                          salog.modify
                      )
                      )
    else:
        parser.print_help()
